﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CH.Easy.Log
{
    public class TxtLog:ILog
    {
        static object o = new object();
        static string path = AppDomain.CurrentDomain.BaseDirectory + @"log\";
        static string sep = Environment.NewLine + "--------------------------------------------------------------------------------------------------";
        public void Write(string text) 
        {
            lock (o)
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                FileStream fs = new FileStream(path + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(text);
                sw.Close();
                sw.Dispose();
                fs.Close();
                fs.Dispose();
            }
        
        }
        public void Write(StringBuilder sb)
        {
            lock (o)
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                FileStream fs = new FileStream(path + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(sb.ToString());
                sw.Close();
                sw.Dispose();
                fs.Close();
                fs.Dispose();
            }

        }

        public bool LogIsNullOrEmpty(object obj)
        {

            StringBuilder sb = new StringBuilder();
           
            bool result = false;
            if (obj == null) 
            {
                sb.Append(sep);
                sb.Append(Environment.NewLine);
                sb.Append("obj=null");
                result = true;
            }

            if (obj.ToString() == string.Empty)
            {
                sb.Append(sep);
                sb.Append(Environment.NewLine);
                sb.Append("obj=Empty");
                result = true;
            }

            if (result)
            {
                sb.Append(Environment.NewLine);
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
            return result;
        }

        public bool LogIsNullOrEmpty(object obj, string msg)
        {
            StringBuilder sb = new StringBuilder();
           
            bool result = false;
            if (obj == null)
            {
                sb.Append(sep);
                sb.Append(Environment.NewLine);
                sb.Append("msg=" + msg);
                sb.Append(Environment.NewLine);
                sb.Append("obj=null");
                result = true;
            }

            if (obj.ToString() == string.Empty)
            {
                sb.Append(sep);
                sb.Append(Environment.NewLine);
                sb.Append("msg=" + msg);
                sb.Append(Environment.NewLine);
                sb.Append("obj=Empty");
                result = true;
            }

            if (result)
            {
                sb.Append(Environment.NewLine);
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
            return result;
        }

        public bool LogIsEquals(object a, object b)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            bool result = false;
            if (a == null)
            {
                sb.Append("a=null");
            }
            if (b == null)
            {
                sb.Append(Environment.NewLine);
                sb.Append("a=null");
            }

            if (a != null && b != null)
            {
                if (a.Equals(b))
                {
                    sb.Append(Environment.NewLine);
                    sb.Append("a(" + a + ")=b(" + b + ")");
                    result = true;
                }
                else
                {
                    sb.Append(Environment.NewLine);
                    sb.Append("a(" + a + ")!=b(" + b + ")");
                    result = false;
                }
            }

            sb.Append(Environment.NewLine);
            sb.Append("时间=" + DateTime.Now);
            this.Write(sb);
            return result;
        }

        public bool LogIsEquals(object a, object b, string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            sb.Append("msg=" + msg);
            bool result = false;
            if (a == null)
            {
                sb.Append("a=null");
            }
            if (b == null)
            {
                sb.Append(Environment.NewLine);
                sb.Append("a=null");
            }

            if (a != null && b != null)
            {
                if (a.Equals(b))
                {
                    sb.Append(Environment.NewLine);
                    sb.Append("a(" + a + ")=b(" + b + ")");
                    result = true;
                }
                else
                {
                    sb.Append(Environment.NewLine);
                    sb.Append("a(" + a + ")!=b(" + b + ")");
                    result = false;
                }
            }

            sb.Append(Environment.NewLine);
            sb.Append("时间=" + DateTime.Now);
            this.Write(sb);
            return result;
        }

        public void Log(Exception e, params object[] param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (e != null)
            {
                sb.Append("e.Message=" + e.Message);
                sb.Append(Environment.NewLine);
                sb.Append("e.StackTrace=" + e.StackTrace);
                sb.Append(Environment.NewLine);
                sb.Append("e.Source=" + e.Source);
                sb.Append(Environment.NewLine);
                sb.Append("e.TargetSite=" + e.TargetSite);
                sb.Append(Environment.NewLine);
                if (e.InnerException != null)
                {
                    sb.Append("e.InnerException=" + e.InnerException.ToString());
                }
                if (param != null) 
                {
                    foreach (var item in param)
                    {
                        sb.Append(Environment.NewLine);
                        sb.Append(item.GetType().Name + "=" + item);
                    }
                }
                sb.Append(Environment.NewLine);
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }

        public void Log(Exception e, string uid, params object[] param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (e != null)
            {
                sb.Append("uid=" + uid);
                sb.Append(Environment.NewLine);
                sb.Append("e.Message=" + e.Message);
                sb.Append(Environment.NewLine);
                sb.Append("e.StackTrace=" + e.StackTrace);
                sb.Append(Environment.NewLine);
                sb.Append("e.Source=" + e.Source);
                sb.Append(Environment.NewLine);
                sb.Append("e.TargetSite=" + e.TargetSite);
                sb.Append(Environment.NewLine);
                if (e.InnerException != null)
                {
                    sb.Append("e.InnerException=" + e.InnerException.ToString());
                }
                if (param != null)
                {
                    foreach (var item in param)
                    {
                        sb.Append(Environment.NewLine);
                        sb.Append(item.GetType().Name + "=" + item);
                    }
                }
                sb.Append(Environment.NewLine);
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }

        public void Log(Exception e, string uid, string name, params object[] param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (e != null)
            {
                sb.Append("uid=" + uid);
                sb.Append(Environment.NewLine);
                sb.Append("name=" + name);
                sb.Append(Environment.NewLine);
                sb.Append("e.Message=" + e.Message);
                sb.Append(Environment.NewLine);
                sb.Append("e.StackTrace=" + e.StackTrace);
                sb.Append(Environment.NewLine);
                sb.Append("e.Source=" + e.Source);
                sb.Append(Environment.NewLine);
                sb.Append("e.TargetSite=" + e.TargetSite);
                sb.Append(Environment.NewLine);
                if (e.InnerException != null)
                {
                    sb.Append("e.InnerException=" + e.InnerException.ToString());
                }
                if (param != null)
                {
                    foreach (var item in param)
                    {
                        sb.Append(Environment.NewLine);
                        sb.Append(item.GetType().Name + "=" + item);
                    }
                }
                sb.Append(Environment.NewLine);
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }

        public void Log(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (!string.IsNullOrEmpty(msg))
            {
                sb.Append("msg=" + msg);
                sb.Append(Environment.NewLine);
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }

        public void Log(string msg, LogType type, params object[] param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (!string.IsNullOrEmpty(msg))
            {
                sb.Append("msg=" + msg);
                sb.Append(Environment.NewLine);
                sb.Append("type=" + Enum.GetName(typeof(LogType), type));
                if (param != null)
                {
                    foreach (var item in param)
                    {
                        sb.Append(Environment.NewLine);
                        sb.Append(item.GetType().Name + "=" + item);
                    }
                }
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }

        public void Log(string msg, LogType type, string uid, params object[] param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (!string.IsNullOrEmpty(msg))
            {
                sb.Append("msg=" + msg);
                sb.Append(Environment.NewLine);
                sb.Append("uid=" + uid);
                sb.Append(Environment.NewLine);
                sb.Append("type=" + Enum.GetName(typeof(LogType), type));
                if (param != null)
                {
                    foreach (var item in param)
                    {
                        sb.Append(Environment.NewLine);
                        sb.Append(item.GetType().Name + "=" + item);
                    }
                }
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }

        public void Log(string msg, LogType type, string uid, string name, params object[] param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (!string.IsNullOrEmpty(msg))
            {
                sb.Append("msg=" + msg);
                sb.Append(Environment.NewLine);
                sb.Append("uid=" + uid);
                sb.Append(Environment.NewLine);
                sb.Append("name=" + name);
                sb.Append(Environment.NewLine);
                sb.Append("type=" + Enum.GetName(typeof(LogType), type));
                if (param != null)
                {
                    foreach (var item in param)
                    {
                        sb.Append(Environment.NewLine);
                        sb.Append(item.GetType().Name + "=" + item);
                    }
                }
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }

        public void Log(string msg, int type, params object[] param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (!string.IsNullOrEmpty(msg))
            {
                sb.Append("msg=" + msg);
                sb.Append(Environment.NewLine);
                sb.Append("type=" + Enum.GetName(typeof(LogType), type));
                if (param != null)
                {
                    foreach (var item in param)
                    {
                        sb.Append(Environment.NewLine);
                        sb.Append(item.GetType().Name + "=" + item);
                    }
                }
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }

        public void Log(string msg, int type, string uid, params object[] param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (!string.IsNullOrEmpty(msg))
            {
                sb.Append("msg=" + msg);
                sb.Append(Environment.NewLine);
                sb.Append("uid=" + uid);
                sb.Append(Environment.NewLine);
                sb.Append("type=" + Enum.GetName(typeof(LogType), type));
                if (param != null)
                {
                    foreach (var item in param)
                    {
                        sb.Append(Environment.NewLine);
                        sb.Append(item.GetType().Name + "=" + item);
                    }
                }
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }

        public void Log(string msg, int type, string uid, string name, params object[] param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sep);
            sb.Append(Environment.NewLine);
            if (!string.IsNullOrEmpty(msg))
            {
                sb.Append("msg=" + msg);
                sb.Append(Environment.NewLine);
                sb.Append("uid=" + uid);
                sb.Append(Environment.NewLine);
                sb.Append("name=" + name);
                sb.Append(Environment.NewLine);
                sb.Append("type=" + Enum.GetName(typeof(LogType), type));
                if (param != null)
                {
                    foreach (var item in param)
                    {
                        sb.Append(Environment.NewLine);
                        sb.Append(item.GetType().Name + "=" + item);
                    }
                }
                sb.Append("时间=" + DateTime.Now);
                this.Write(sb);
            }
        }
    }
}
